// pages/detail/detail.js
// pages/detail/detail.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lightdata1:'light',
    lightdata2:'unlight',
    lightdata3:'unlight',
    isshow1:'appear',
    isshow2:'dis',
    isshow3:'dis',

    itemDetail:{}, 
    imgCollect:false,
    image1:'/images/collect.png',
    image2:'/images/collectTrue.png',
    wid:'',
    uid:'',
    dataDetail:'',
    itemDetail:''
    
  },

 
  //点击收藏
  changeCollect: function(){
    if(wx.getStorageSync('uid')){  //已登录
      console.log("uid:",wx.getStorageSync('uid'));

    this.setData({
        Uimage:wx.getStorageSync('Uimage'),
        Uname:wx.getStorageSync('Uname'),
        uid:wx.getStorageSync('uid'),
        isLogin:true
    }) 

     let isCollect = this.data.imgCollect;
      this.setData({
        imgCollect:!isCollect
      })
      let that = this;
      console.log(this.data.uid)
    wx.request({  //收藏至数据库
      url: `${app.globalData.url}/recommend/content`,//写自己的服务器
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: "POST",
      data: {/* that.data.Uid */
        uid:that.data.uid,
        wid:that.data.wid
       
      },
      success: function (res) { 
        console.log(res) ;
        
      
      },
      fail: function (res) {
        console.log("fail")
        console.log(res)          
      }

  })
  }
  else{

    console.log('未登录')
    wx.showModal({
      title: '提示',
      content: '登录后收藏',
      showCancel: true, 
      cancelText: "不登录", 
      cancelColor: ' rgb(178, 89, 75)', 
      confirmText: "去登录", 
      confirmColor: 'rgb(178, 89, 75)', 
      success: function(res) {
        if (res.cancel) {
       
         
        } else if(res.confirm){
          
          wx.navigateTo({
            url: '../../pages/User/User',
          })
        }
      }
    })
  }
    
  


    detailnum:""

  },
  
  change1: function(){
    this.setData({
      lightdata1: 'light',
      lightdata2: 'unlight',
      lightdata3: 'unlight',
      isshow1: 'appear',
      isshow2: 'dis',
      isshow3: 'dis' 
    })
  }, 
  change2: function(){

    this.setData({
      lightdata1: 'unlight',
      lightdata2: 'light',
      lightdata3: 'unlight',
      isshow1: 'dis',
      isshow2: 'appear',
      isshow3: 'dis'         
    })
  }, 
  change3: function(){
    this.setData({
      lightdata1: 'unlight',
      lightdata2: 'unlight',
      lightdata3: 'light',
      isshow1: 'dis',
      isshow2: 'dis',
      isshow3: 'appear'           
    })
  }, 
  

  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.wid = options.wid
    var self = this
    console.log(options)
    wx.request({
      url: 'https://wx.original5.xyz:2003/recommend/content', 
      data: {
        wid:self.data.wid
      },
      method:'GET',
      header: {
        'content-type': 'application/json' 
      },
      success: function (res) {
        console.log(res.data)
        let result = res.data;
          self.setData({
            dataDetail:result[0]
          })
        console.log(result[0])
        console.log(self.data.dataDetail)
      }
    })
   
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this; 
   
    
    setTimeout(function(){
      console.log(that.data.dataDetail)
      var dataDetail =that.data.dataDetail
  
      console.log(that.data.dataDetail)
      let content = dataDetail.wcontent;
      let count = 0;
      for(var i=0;i<content.length;i++) {
        if(content[i] === "。") {
          count++;
        }
      }
      // 
      if((count%2) ===0 ) {

        //原文
        var sentence = dataDetail.wcontent.split("。");
        for(var j=0;j<sentence.length-1;j++) {
          sentence[j] = sentence[j]+"。"
        }
        console.log(sentence)
        var len = (sentence.length-1)/2;
        sentence.splice(len,0,"\n&emsp;&emsp;");
        console.log(sentence)
        dataDetail.wcontent = sentence.join("");
  
        //翻译
        var trans = dataDetail.wtranslate.split("。");
        for(var k=0;k<trans.length-1;k++) {
          trans[k] = trans[k]+"。"
          
        }
        var len = (trans.length-1)/2;
        trans.splice(len,0,"\n&emsp;&emsp;");
        console.log(trans)
        dataDetail.wtranslate = trans.join("");

      }
      //赏析
      var appre = dataDetail.wappreciation.split("。");
      for(var q=0;q<appre.length-1;q++) {
        appre[q] = appre[q]+"。"
        
      }
      var len = (appre.length-1)/2;
      appre.splice(len,0,"\n&emsp;&emsp;");
      console.log(appre)
      dataDetail.wappreciation = appre.join("");

      

      
      that.setData({
        itemDetail:dataDetail
      })
    },100)
    
    
    wx.request({   //从数据库获取收藏
      url: `${app.globalData.url}/users/my-collect`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        uid: wx.getStorageSync('uid')
      },
    
      success: function(res) { 

        for(var i=0;i<res.data.length;i++){
          console.log(res.data[i].wid)
          if(res.data[i].wid ==that.data.wid ){
            that.setData({
              imgCollect:true
            })
          }
        }
        // that.setData({
        //   imgCollect:false
        // })
        
      
      }
      

      
    })
    
    
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})



